//
//  YLLVisitorView.swift
//  YLLSina
//
//  Created by 于玲玲 on 16/7/28.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


protocol YLLVisitorViewDelegate:NSObjectProtocol {
    
    func didLoginMethod()
    func didRegisterMethod()
    
}


class YLLVisitorView: UIView {
//为什么加问号
    weak var delegate: YLLVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(homeIconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
        homeIconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
        turnImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
        }
        
        // 注册按钮 -- 一般的约束是 4个
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }

    }
    
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        
        animation.removedOnCompletion = false
        
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    
    
    
    func setupInfo(imageName: String,message: String,isHome: Bool = false) {
        
        homeIconImageView.image = UIImage(named: imageName)
        messageLabel.text = message
        if isHome {
            turnImageView.hidden = false
            startAnimation()
        }else{
            turnImageView.hidden = true
        }
        
        
    }
    
    
    
    
    
    
    //MARK:-- 按钮的点击
    func register(){
        
        print("按钮点击了")
        delegate?.didRegisterMethod()
    }
    
    func login(){
        
        print("登录")
        delegate?.didLoginMethod()
    }

    
    
    
    //第三步 懒加载控件
    //MARK：--懒加载控件
    
    //蒙板
    private lazy var maskImageView: UIImageView = {
        
        let img  = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
    //首页的小房子
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    
    //转动图片
    private lazy var turnImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        
    }()
    
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "关注一些人"
        
        label.textAlignment = .Center
        
        return label
        
        
    }()
    
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        return button
        
        
    }()
    
    ///注册按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()


    
    
    
}
